<template>
  <div class="hello">
    <el-dialog title="编辑学生信息"
               :visible.sync="dialogEdit.show">
      <el-form :model="formData"
               ref="formEdit"
               label-width="100px"
               :rules="formRules">
        <el-form-item label="姓名"
                      prop="name">
          <el-input v-model="formData.name"></el-input>
        </el-form-item>
        <el-form-item label="学号"
                      prop="sno">
          <el-input v-model="formData.sno"></el-input>
        </el-form-item>
        <el-form-item label="班级"
                      prop="classes">
          <el-input v-model="formData.classes"></el-input>
        </el-form-item>
        <el-form-item label="学校"
                      prop="school">
          <el-input v-model="formData.school"
                    :disabled="true"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogEdit.show = false">取 消</el-button>
        <el-button type="primary"
                   @click="dialogFormEdit('formEdit')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from '../axios.js'
export default {
  name: 'EditUser',
  props: {
    dialogEdit: Object,
    formData: Object,
  },
  data () {
    return {
      formRules: {
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        sno: [{ required: true, message: '学号不能为空', trigger: 'blur' }],
        classes: [
          { required: true, message: '班级不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    dialogFormEdit (formEdit) {
      this.$refs[formEdit].validate((valid) => {
        if (valid) {
          axios.setStudentInfo(this.formData.student_id, this.formData).then((res) => {
            this.$message({
              type: 'success',
              message: '编辑信息成功',
            })
            console.log(res)
            this.dialogEdit.show = false
            this.$emit('updateEdit')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped></style>
